// MajorController.java
package com.question.controller.admin;

import com.question.pojo.dto.MajorDTO;
import com.question.pojo.entity.Major;
import com.question.pojo.result.Result;
import com.question.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/manage/majors")
@Slf4j
@Api(tags = "专业管理相关接口")
public class MajorController {

    @Autowired
    private MajorService majorService;

    /**
     * 获取所有专业
     *
     * @return
     */
    @GetMapping
    @ApiOperation("获取所有专业")
    public Result<List<Major>> getAllMajors() {
        List<Major> majors = majorService.getAllMajors();
        return Result.success(majors);
    }

    @PostMapping
    @ApiOperation("添加专业")
    public Result<Void> addMajor(@RequestBody MajorDTO majorDTO) {
        Major major = new Major();
        major.setMajorName(majorDTO.getMajorName());
        major.setDescription(majorDTO.getDescription());
        majorService.addMajor(major);
        return Result.success();
    }

    @PutMapping("/{majorId}")
    @ApiOperation("更新专业")
    public Result<Void> updateMajor(@PathVariable Integer majorId, @RequestBody MajorDTO majorDTO) {
        Major major = new Major();
        major.setMajorId(majorId);
        major.setMajorName(majorDTO.getMajorName());
        major.setDescription(majorDTO.getDescription());
        majorService.updateMajor(major);
        return Result.success();
    }

    @DeleteMapping("/{majorId}")
    @ApiOperation("删除专业")
    public Result<Void> deleteMajor(@PathVariable Integer majorId) {
        majorService.deleteMajor(majorId);
        return Result.success();
    }

    @GetMapping("/{majorId}")
    @ApiOperation("根据ID获取专业")
    public Result<Major> getMajorById(@PathVariable Integer majorId) {
        Major major = majorService.getMajorById(majorId);
        return Result.success(major);
    }
}